﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Watermelon
{
    public class CurrenciesUIController : MonoBehaviour
    {
        [SerializeField] GameObject panelObject;
        [SerializeField] Transform parentTrasnform;

        private Pool panelPool;

        private Dictionary<Currency.Type, CurrencyUI> activePanelsUI;

        private VerticalLayoutGroup verticalLayoutGroup;

        private void Awake()
        {
            panelPool = new Pool(new PoolSettings("Currency Panel", panelObject, 3, true, parentTrasnform));

            verticalLayoutGroup = GetComponent<VerticalLayoutGroup>();
        }

        public void Initialise(Currency[] currencies)
        {
            activePanelsUI = new Dictionary<Currency.Type, CurrencyUI>();
            for (int i = 0; i < currencies.Length; i++)
            {
                if(currencies[i].DisplayAlways)
                {
                    GameObject currencyObject = panelPool.GetPooledObject();
                    currencyObject.transform.SetParent(parentTrasnform);
                    currencyObject.transform.ResetLocal();
                    currencyObject.transform.SetAsLastSibling();
                    currencyObject.SetActive(true);

                    CurrencyUI currencyUI = currencyObject.GetComponent<CurrencyUI>();
                    currencyUI.Initialise(currencies[i]);

                    activePanelsUI.Add(currencies[i].CurrencyType, currencyUI);
                }
            }
        }

        public void RedrawCurrency(Currency.Type type, int amount)
        {
            if(activePanelsUI.ContainsKey(type))
            {
                activePanelsUI[type].SetAmount(amount);
            }
        }

        public void ActivateCurrency(Currency.Type type)
        {

        }
    }
}